package com.fqgj.sentry.policy.engine.resolver;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fqgj.sentry.policy.engine.domain.PolicySet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class CheckParser {


    /**
     * 读取txt文件的内容
     * @param file 想要读取的文件对象
     * @return 返回文件内容
     */
    public static String file2String(File file){

        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                result.append(System.lineSeparator()+s);
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }

    /**
     *
     * @return
     */
    public  static  ObjectMapper getJsonMapper() {
        ObjectMapper mapper = new ObjectMapper();

        ObjectMapper configure = mapper.configure(SerializationFeature.INDENT_OUTPUT, Boolean.TRUE);

        return configure;
    }

    /**
     *
     * @return
     */
    public static PolicySet json2PolicySet() {
        String json = file2String(new File("/Users/kennykong/Documents/workspace/risk-sentry/policy-engine/target/classes/policies/policyTestParser1.json"));
        ObjectMapper mapper = getJsonMapper();
        PolicySet ps = null;
        try {
            ps = mapper.readValue(json, PolicySet.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ps;
    }


    public static void main(String[] args) {
        PolicySet ps = json2PolicySet();

        Parser par = new Parser();
        PolicySet ps1  = par.processPolicySet(ps);

        ObjectMapper mapper = getJsonMapper();
        try {
            System.out.println(mapper.writeValueAsString(ps1));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

}
